from typing import Dict
from x_com.import_api import ecommerce_data_source_service_0_0_1
from utils import time_utils

class UserRegisterAnalyzer:
    # 年度数据
    class YearData:
        # 每月增加的用户
        monthly_num_dict: Dict[int, int]
        def __init__(self):
            self.monthly_num_dict = {}
    # 总人数
    total_num: int
    # 每月新增的用户数据
    # key 为 年份
    year_data_dict : Dict[int, YearData]

    def __init__(self):
        self.total_num = 0
        self.year_data_dict = {}

    def data_in(self, param: ecommerce_data_source_service_0_0_1.UserInfo):
        self.total_num += 1

        datetime_obj = time_utils.str_to_datetime(param.register_time)
        # 数据所在的年份
        year = datetime_obj.year
        # 年数据
        if year not in self.year_data_dict:
            self.year_data_dict[year] = self.YearData()
        year_data = self.year_data_dict[year]
        month = datetime_obj.month
        if month not in year_data.monthly_num_dict:
            year_data.monthly_num_dict[month] = 1
        else:
            year_data.monthly_num_dict[month] += 1

    def get_total_num(self) -> int:
        return self.total_num
    
    def get_data_by_year(self, year: int) -> YearData:
        return self.year_data_dict.get(year)